<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../JSONPHook-v1.0.min.js"></script>
</head>
<body>

</body>
<script>
    /*function JSONPHook(urls,hookCallBack){
        function isExit(url){
            let ret=false;
            urls.forEach((e)=>{
                if(url.indexOf(e)){
                    ret=true;
                }
            })
            return ret;
        }
        hook(document, "createElement", function(e) {
            let tag=this;
            if(e==='script'){
                Object.defineProperty(tag, 'src', {
                    configurable: true,
                    enumerable: true,
                    set: function(n) {
                        //拿到链接，判断是为hook链接
                        if(isExit(n)){
                            // 设置callback
                            let baseCallbackName=n.match(/(?<=callback=)jsonp[\d]*!/)[0];
                            let callbackName='jsonp'+new Date().getTime();
                            window[callbackName]=function(json){
                                let args = [].slice.call(arguments);
                                //调用callback
                                if(hookCallBack)hookCallBack.apply(this,args);
                                //调用返回的
                                if(typeof window[baseCallbackName]==='function')window[baseCallbackName].apply(this,args);
                                window[callbackName]=undefined;
                            };
                            n=n.replace(baseCallbackName,callbackName);
                        }
                        //给真正的tag赋值
                        this.setAttribute('src',n)
                    },
                    get:function(){
                        return this.getAttribute('src');
                    }
                });
            }
            return tag;
        },false);
    }*/
    JSONPHook(['alicdn.com'],function(json){
        console.log(json);
    });
    //取消hook
    //unhook(document, "createElement");
    //document.createElement=createElement;
    function jsonp(options) {
        options = options || {};
        if (!options.url || !options.callback) {
            throw new Error("参数不合法");
        }

        //创建 script 标签并加入到页面中
        var callbackName = ('jsonp' + Math.random()).replace(".", "");
        var oHead = document.getElementsByTagName('head')[0];
        options.data[options.callback] = callbackName;
        var params = formatParams(options.data);
        var oS = document.createElement('script');
        oHead.appendChild(oS);

        //创建jsonp回调函数
        window[callbackName] = function (json) {
            oHead.removeChild(oS);
            clearTimeout(oS.timer);
            window[callbackName] = null;
            options.success && options.success(json);
        };

        //发送请求
        oS.src = options.url + '?' + params;

        //超时处理
        if (options.time) {
            oS.timer = setTimeout(function () {
                window[callbackName] = null;
                oHead.removeChild(oS);
                options.fail && options.fail({ message: "超时" });
            }, time);
        }
    }
    //格式化参数
    function formatParams(data) {
        var arr = [];
        for (var name in data) {
            arr.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name]));
        }
        return arr.join('&');
    }
    jsonp({
        url:'https://t.alicdn.com/t/gettime',
        data:{
            _ksTS:'1555983846865_1101'
        },
        callback:'callback',
        success(e) {
            alert('完成')
        },fail() {
            alert('超时')
        }
    })
</script>
</html>